<!DOCTYPE html />

<html>
<head>
	<title>Driver.cs</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<link href="../nocco.css" rel="stylesheet" media="all" type="text/css" />
	<script src="../prettify.js" type="text/javascript"></script>
</head>
<body onload="prettyPrint()">
	<div id="container">
		<div id="background"></div>
			<div id="jump_to">
				Jump To &hellip;
				<div id="jump_wrapper">
					<div id="jump_page">
							<a class="source" href="../nmodbus4/globalsuppressions.html">
								NModbus4\GlobalSuppressions.cs
							</a>
							<a class="source" href="../nmodbus4/invalidmodbusrequestexception.html">
								NModbus4\InvalidModbusRequestException.cs
							</a>
							<a class="source" href="../nmodbus4/modbus.html">
								NModbus4\Modbus.cs
							</a>
							<a class="source" href="../nmodbus4/slaveexception.html">
								NModbus4\SlaveException.cs
							</a>
							<a class="source" href="../nmodbus4/data/datastore.html">
								NModbus4\Data\DataStore.cs
							</a>
							<a class="source" href="../nmodbus4/data/datastoreeventargs.html">
								NModbus4\Data\DataStoreEventArgs.cs
							</a>
							<a class="source" href="../nmodbus4/data/datastorefactory.html">
								NModbus4\Data\DataStoreFactory.cs
							</a>
							<a class="source" href="../nmodbus4/data/discretecollection.html">
								NModbus4\Data\DiscreteCollection.cs
							</a>
							<a class="source" href="../nmodbus4/data/imodbusmessagedatacollection.html">
								NModbus4\Data\IModbusMessageDataCollection.cs
							</a>
							<a class="source" href="../nmodbus4/data/modbusdatacollection.html">
								NModbus4\Data\ModbusDataCollection.cs
							</a>
							<a class="source" href="../nmodbus4/data/modbusdatatype.html">
								NModbus4\Data\ModbusDataType.cs
							</a>
							<a class="source" href="../nmodbus4/data/registercollection.html">
								NModbus4\Data\RegisterCollection.cs
							</a>
							<a class="source" href="../nmodbus4/device/imodbusmaster.html">
								NModbus4\Device\IModbusMaster.cs
							</a>
							<a class="source" href="../nmodbus4/device/imodbusserialmaster.html">
								NModbus4\Device\IModbusSerialMaster.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusdevice.html">
								NModbus4\Device\ModbusDevice.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusipmaster.html">
								NModbus4\Device\ModbusIpMaster.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusmaster.html">
								NModbus4\Device\ModbusMaster.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusmastertcpconnection.html">
								NModbus4\Device\ModbusMasterTcpConnection.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusserialmaster.html">
								NModbus4\Device\ModbusSerialMaster.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusserialslave.html">
								NModbus4\Device\ModbusSerialSlave.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusslave.html">
								NModbus4\Device\ModbusSlave.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusslaverequesteventargs.html">
								NModbus4\Device\ModbusSlaveRequestEventArgs.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbustcpslave.html">
								NModbus4\Device\ModbusTcpSlave.cs
							</a>
							<a class="source" href="../nmodbus4/device/modbusudpslave.html">
								NModbus4\Device\ModbusUdpSlave.cs
							</a>
							<a class="source" href="../nmodbus4/device/tcpconnectioneventargs.html">
								NModbus4\Device\TcpConnectionEventArgs.cs
							</a>
							<a class="source" href="../nmodbus4/extensions/enron/enronmodbus.html">
								NModbus4\Extensions\Enron\EnronModbus.cs
							</a>
							<a class="source" href="../nmodbus4/io/emptytransport.html">
								NModbus4\IO\EmptyTransport.cs
							</a>
							<a class="source" href="../nmodbus4/io/istreamresource.html">
								NModbus4\IO\IStreamResource.cs
							</a>
							<a class="source" href="../nmodbus4/io/modbusasciitransport.html">
								NModbus4\IO\ModbusAsciiTransport.cs
							</a>
							<a class="source" href="../nmodbus4/io/modbusiptransport.html">
								NModbus4\IO\ModbusIpTransport.cs
							</a>
							<a class="source" href="../nmodbus4/io/modbusrtutransport.html">
								NModbus4\IO\ModbusRtuTransport.cs
							</a>
							<a class="source" href="../nmodbus4/io/modbusserialtransport.html">
								NModbus4\IO\ModbusSerialTransport.cs
							</a>
							<a class="source" href="../nmodbus4/io/modbustransport.html">
								NModbus4\IO\ModbusTransport.cs
							</a>
							<a class="source" href="../nmodbus4/io/serialportadapter.html">
								NModbus4\IO\SerialPortAdapter.cs
							</a>
							<a class="source" href="../nmodbus4/io/streamresourceutility.html">
								NModbus4\IO\StreamResourceUtility.cs
							</a>
							<a class="source" href="../nmodbus4/io/tcpclientadapter.html">
								NModbus4\IO\TcpClientAdapter.cs
							</a>
							<a class="source" href="../nmodbus4/io/udpclientadapter.html">
								NModbus4\IO\UdpClientAdapter.cs
							</a>
							<a class="source" href="../nmodbus4/message/abstractmodbusmessage.html">
								NModbus4\Message\AbstractModbusMessage.cs
							</a>
							<a class="source" href="../nmodbus4/message/abstractmodbusmessagewithdata.html">
								NModbus4\Message\AbstractModbusMessageWithData.cs
							</a>
							<a class="source" href="../nmodbus4/message/diagnosticsrequestresponse.html">
								NModbus4\Message\DiagnosticsRequestResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/imodbusmessage.html">
								NModbus4\Message\IModbusMessage.cs
							</a>
							<a class="source" href="../nmodbus4/message/imodbusrequest.html">
								NModbus4\Message\IModbusRequest.cs
							</a>
							<a class="source" href="../nmodbus4/message/modbusmessagefactory.html">
								NModbus4\Message\ModbusMessageFactory.cs
							</a>
							<a class="source" href="../nmodbus4/message/modbusmessageimpl.html">
								NModbus4\Message\ModbusMessageImpl.cs
							</a>
							<a class="source" href="../nmodbus4/message/readcoilsinputsrequest.html">
								NModbus4\Message\ReadCoilsInputsRequest.cs
							</a>
							<a class="source" href="../nmodbus4/message/readcoilsinputsresponse.html">
								NModbus4\Message\ReadCoilsInputsResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/readholdinginputregistersrequest.html">
								NModbus4\Message\ReadHoldingInputRegistersRequest.cs
							</a>
							<a class="source" href="../nmodbus4/message/readholdinginputregistersresponse.html">
								NModbus4\Message\ReadHoldingInputRegistersResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/readwritemultipleregistersrequest.html">
								NModbus4\Message\ReadWriteMultipleRegistersRequest.cs
							</a>
							<a class="source" href="../nmodbus4/message/slaveexceptionresponse.html">
								NModbus4\Message\SlaveExceptionResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/writemultiplecoilsrequest.html">
								NModbus4\Message\WriteMultipleCoilsRequest.cs
							</a>
							<a class="source" href="../nmodbus4/message/writemultiplecoilsresponse.html">
								NModbus4\Message\WriteMultipleCoilsResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/writemultipleregistersrequest.html">
								NModbus4\Message\WriteMultipleRegistersRequest.cs
							</a>
							<a class="source" href="../nmodbus4/message/writemultipleregistersresponse.html">
								NModbus4\Message\WriteMultipleRegistersResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/writesinglecoilrequestresponse.html">
								NModbus4\Message\WriteSingleCoilRequestResponse.cs
							</a>
							<a class="source" href="../nmodbus4/message/writesingleregisterrequestresponse.html">
								NModbus4\Message\WriteSingleRegisterRequestResponse.cs
							</a>
							<a class="source" href="../nmodbus4/properties/assemblyinfo.html">
								NModbus4\Properties\AssemblyInfo.cs
							</a>
							<a class="source" href="../nmodbus4/unme.common/disposableutility.html">
								NModbus4\Unme.Common\DisposableUtility.cs
							</a>
							<a class="source" href="../nmodbus4/unme.common/eventutility.html">
								NModbus4\Unme.Common\EventUtility.cs
							</a>
							<a class="source" href="../nmodbus4/unme.common/sequenceutility.html">
								NModbus4\Unme.Common\SequenceUtility.cs
							</a>
							<a class="source" href="../nmodbus4/utility/discriminatedunion.html">
								NModbus4\Utility\DiscriminatedUnion.cs
							</a>
							<a class="source" href="../nmodbus4/utility/modbusutility.html">
								NModbus4\Utility\ModbusUtility.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/slaveexceptionfixture.html">
								NModbus4.UnitTests\SlaveExceptionFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/data/datastoreeventargsfixture.html">
								NModbus4.UnitTests\Data\DataStoreEventArgsFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/data/datastorefixture.html">
								NModbus4.UnitTests\Data\DataStoreFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/data/discretecollectionfixture.html">
								NModbus4.UnitTests\Data\DiscreteCollectionFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/data/modbusdatacollectionfixture.html">
								NModbus4.UnitTests\Data\ModbusDataCollectionFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/data/registercollectionfixture.html">
								NModbus4.UnitTests\Data\RegisterCollectionFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/device/modbusmasterfixture.html">
								NModbus4.UnitTests\Device\ModbusMasterFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/device/modbusslavefixture.html">
								NModbus4.UnitTests\Device\ModbusSlaveFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/device/tcpconnectioneventargsfixture.html">
								NModbus4.UnitTests\Device\TcpConnectionEventArgsFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/io/modbusasciitransportfixture.html">
								NModbus4.UnitTests\IO\ModbusAsciiTransportFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/io/modbusrtutransportfixture.html">
								NModbus4.UnitTests\IO\ModbusRtuTransportFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/io/modbusserialtransportfixture.html">
								NModbus4.UnitTests\IO\ModbusSerialTransportFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/io/modbustcptransportfixture.html">
								NModbus4.UnitTests\IO\ModbusTcpTransportFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/io/modbustransportfixture.html">
								NModbus4.UnitTests\IO\ModbusTransportFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/io/udpclientadapterfixture.html">
								NModbus4.UnitTests\IO\UdpClientAdapterFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/diagnosticsrequestresponsefixture.html">
								NModbus4.UnitTests\Message\DiagnosticsRequestResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/messageutility.html">
								NModbus4.UnitTests\Message\MessageUtility.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/modbusmessagefactoryfixture.html">
								NModbus4.UnitTests\Message\ModbusMessageFactoryFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/modbusmessagefixture.html">
								NModbus4.UnitTests\Message\ModbusMessageFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/modbusmessageimplfixture.html">
								NModbus4.UnitTests\Message\ModbusMessageImplFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/modbusmessagewithdatafixture.html">
								NModbus4.UnitTests\Message\ModbusMessageWithDataFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/readcoilsinputsrequestfixture.html">
								NModbus4.UnitTests\Message\ReadCoilsInputsRequestFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/readcoilsinputsresponsefixture.html">
								NModbus4.UnitTests\Message\ReadCoilsInputsResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/readholdinginputregistersrequestfixture.html">
								NModbus4.UnitTests\Message\ReadHoldingInputRegistersRequestFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/readholdinginputregistersresponsefixture.html">
								NModbus4.UnitTests\Message\ReadHoldingInputRegistersResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/readwritemultipleregistersrequestfixture.html">
								NModbus4.UnitTests\Message\ReadWriteMultipleRegistersRequestFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/returnquerydatarequestresponsefixture.html">
								NModbus4.UnitTests\Message\ReturnQueryDataRequestResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/slaveexceptionresponsefixture.html">
								NModbus4.UnitTests\Message\SlaveExceptionResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/writemultiplecoilsrequestfixture.html">
								NModbus4.UnitTests\Message\WriteMultipleCoilsRequestFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/writemultiplecoilsresponsefixture.html">
								NModbus4.UnitTests\Message\WriteMultipleCoilsResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/writemultipleregistersrequestfixture.html">
								NModbus4.UnitTests\Message\WriteMultipleRegistersRequestFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/writemultipleregistersresponsefixture.html">
								NModbus4.UnitTests\Message\WriteMultipleRegistersResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/writesinglecoilrequestresponsefixture.html">
								NModbus4.UnitTests\Message\WriteSingleCoilRequestResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/message/writesingleregisterrequestresponsefixture.html">
								NModbus4.UnitTests\Message\WriteSingleRegisterRequestResponseFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/properties/assemblyinfo.html">
								NModbus4.UnitTests\Properties\AssemblyInfo.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/utility/collectionutilityfixture.html">
								NModbus4.UnitTests\Utility\CollectionUtilityFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/utility/discriminatedunionfixture.html">
								NModbus4.UnitTests\Utility\DiscriminatedUnionFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/utility/modbusutilityfixture.html">
								NModbus4.UnitTests\Utility\ModbusUtilityFixture.cs
							</a>
							<a class="source" href="../nmodbus4.unittests/utility/serialconnectionfixture.html">
								NModbus4.UnitTests\Utility\SerialConnectionFixture.cs
							</a>
							<a class="source" href="../samples/driver.html">
								Samples\Driver.cs
							</a>
							<a class="source" href="../samples/properties/assemblyinfo.html">
								Samples\Properties\AssemblyInfo.cs
							</a>
					</div>
				</div>
			</div>
		<table cellpadding="0" cellspacing="0">
			<thead>
				<tr>
					<th class="docs">
						<h1>Driver.cs</h1>
					</th>
					<th class="code"></th>
				</tr>
			</thead>
			<tbody>
					<tr id="section_1">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_1">&#182;</a>
							</div>
							
						</td>
						<td class="code">
							<pre><code class='prettyprint'>using System;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;

namespace MySample
{
</code></pre>
						</td>
					</tr>
					<tr id="section_2">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_2">&#182;</a>
							</div>
							<p>Demonstration of NModbus</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>    public class Driver
    {
        private static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure();

            try
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_3">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_3">&#182;</a>
							</div>
							<p>ModbusTcpMasterReadInputs();
SimplePerfTest();
ModbusSerialRtuMasterWriteRegisters();
ModbusSerialAsciiMasterReadRegisters();
ModbusTcpMasterReadInputs();<br />
StartModbusAsciiSlave();
ModbusTcpMasterReadInputsFromModbusSlave();
ModbusSerialAsciiMasterReadRegistersFromModbusSlave();
StartModbusTcpSlave();
StartModbusUdpSlave();
StartModbusAsciiSlave();</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadKey();
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_4">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_4">&#182;</a>
							</div>
							<p>Simple Modbus serial RTU master write holding registers example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ModbusSerialRtuMasterWriteRegisters()
        {
            using (SerialPort port = new SerialPort(&quot;COM1&quot;))
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_5">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_5">&#182;</a>
							</div>
							<p>configure serial port</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

</code></pre>
						</td>
					</tr>
					<tr id="section_6">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_6">&#182;</a>
							</div>
							<p>create modbus master</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);

                byte slaveId = 1;
                ushort startAddress = 100;
                ushort[] registers = new ushort[] { 1, 2, 3 };

</code></pre>
						</td>
					</tr>
					<tr id="section_7">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_7">&#182;</a>
							</div>
							<p>write three registers</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                master.WriteMultipleRegisters(slaveId, startAddress, registers);
            }
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_8">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_8">&#182;</a>
							</div>
							<p>Simple Modbus serial ASCII master read holding registers example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ModbusSerialAsciiMasterReadRegisters()
        {
            using (SerialPort port = new SerialPort(&quot;COM1&quot;))
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_9">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_9">&#182;</a>
							</div>
							<p>configure serial port</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

</code></pre>
						</td>
					</tr>
					<tr id="section_10">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_10">&#182;</a>
							</div>
							<p>create modbus master</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);

                byte slaveId = 1;
                ushort startAddress = 1;
                ushort numRegisters = 5;

</code></pre>
						</td>
					</tr>
					<tr id="section_11">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_11">&#182;</a>
							</div>
							<p>read five registers     </p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                for (int i = 0; i &lt; numRegisters; i++)
                    Console.WriteLine(&quot;Register {0}={1}&quot;, startAddress + i, registers[i]);
            }

</code></pre>
						</td>
					</tr>
					<tr id="section_12">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_12">&#182;</a>
							</div>
							<p>output: 
Register 1=0
Register 2=0
Register 3=0
Register 4=0
Register 5=0</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        }

</code></pre>
						</td>
					</tr>
					<tr id="section_13">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_13">&#182;</a>
							</div>
							<p>Simple Modbus TCP master read inputs example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ModbusTcpMasterReadInputs()
        {
            using (TcpClient client = new TcpClient(&quot;127.0.0.1&quot;, 502))
            {
                ModbusIpMaster master = ModbusIpMaster.CreateIp(client);

</code></pre>
						</td>
					</tr>
					<tr id="section_14">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_14">&#182;</a>
							</div>
							<p>read five input values</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ushort startAddress = 100;
                ushort numInputs = 5;
                bool[] inputs = master.ReadInputs(startAddress, numInputs);

                for (int i = 0; i &lt; numInputs; i++)
                    Console.WriteLine(&quot;Input {0}={1}&quot;, startAddress + i, inputs[i] ? 1 : 0);
            }

</code></pre>
						</td>
					</tr>
					<tr id="section_15">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_15">&#182;</a>
							</div>
							<p>output: 
Input 100=0
Input 101=0
Input 102=0
Input 103=0
Input 104=0</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        }

</code></pre>
						</td>
					</tr>
					<tr id="section_16">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_16">&#182;</a>
							</div>
							<p>Simple Modbus UDP master write coils example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ModbusUdpMasterWriteCoils()
        {
            using (UdpClient client = new UdpClient())
            {
                IPEndPoint endPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 502);
                client.Connect(endPoint);

                ModbusIpMaster master = ModbusIpMaster.CreateIp(client);

                ushort startAddress = 1;

</code></pre>
						</td>
					</tr>
					<tr id="section_17">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_17">&#182;</a>
							</div>
							<p>write three coils</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                master.WriteMultipleCoils(startAddress, new bool[] { true, false, true });
            }
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_18">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_18">&#182;</a>
							</div>
							<p>Simple Modbus serial ASCII slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void StartModbusSerialAsciiSlave()
        {
            using (SerialPort slavePort = new SerialPort(&quot;COM2&quot;))
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_19">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_19">&#182;</a>
							</div>
							<p>configure serial port</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                slavePort.BaudRate = 9600;
                slavePort.DataBits = 8;
                slavePort.Parity = Parity.None;
                slavePort.StopBits = StopBits.One;
                slavePort.Open();

                byte unitId = 1;

</code></pre>
						</td>
					</tr>
					<tr id="section_20">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_20">&#182;</a>
							</div>
							<p>create modbus slave</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ModbusSlave slave = ModbusSerialSlave.CreateAscii(unitId, slavePort);
                slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

                slave.Listen();
            }
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_21">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_21">&#182;</a>
							</div>
							<p>Simple Modbus serial RTU slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void StartModbusSerialRtuSlave()
        {
            using (SerialPort slavePort = new SerialPort(&quot;COM2&quot;))
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_22">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_22">&#182;</a>
							</div>
							<p>configure serial port</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                slavePort.BaudRate = 9600;
                slavePort.DataBits = 8;
                slavePort.Parity = Parity.None;
                slavePort.StopBits = StopBits.One;
                slavePort.Open();

                byte unitId = 1;

</code></pre>
						</td>
					</tr>
					<tr id="section_23">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_23">&#182;</a>
							</div>
							<p>create modbus slave</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ModbusSlave slave = ModbusSerialSlave.CreateRtu(unitId, slavePort);
                slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

                slave.Listen();
            }
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_24">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_24">&#182;</a>
							</div>
							<p>Simple Modbus serial USB ASCII slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void StartModbusSerialUsbAsciiSlave()
        {
</code></pre>
						</td>
					</tr>
					<tr id="section_25">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_25">&#182;</a>
							</div>
							<p>TODO</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        }

</code></pre>
						</td>
					</tr>
					<tr id="section_26">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_26">&#182;</a>
							</div>
							<p>Simple Modbus serial USB RTU slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void StartModbusSerialUsbRtuSlave()
        {
</code></pre>
						</td>
					</tr>
					<tr id="section_27">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_27">&#182;</a>
							</div>
							<p>TODO</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        }

</code></pre>
						</td>
					</tr>
					<tr id="section_28">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_28">&#182;</a>
							</div>
							<p>Simple Modbus TCP slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void StartModbusTcpSlave()
        {
            byte slaveId = 1;
            int port = 502;
            IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });

</code></pre>
						</td>
					</tr>
					<tr id="section_29">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_29">&#182;</a>
							</div>
							<p>create and start the TCP slave</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            TcpListener slaveTcpListener = new TcpListener(address, port);
            slaveTcpListener.Start();

            ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveId, slaveTcpListener);
            slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

            slave.Listen();

</code></pre>
						</td>
					</tr>
					<tr id="section_30">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_30">&#182;</a>
							</div>
							<p>prevent the main thread from exiting</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            Thread.Sleep(Timeout.Infinite);
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_31">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_31">&#182;</a>
							</div>
							<p>Simple Modbus UDP slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void StartModbusUdpSlave()
        {
            using (UdpClient client = new UdpClient(502))
            {
                ModbusUdpSlave slave = ModbusUdpSlave.CreateUdp(client);
                slave.DataStore = DataStoreFactory.CreateDefaultDataStore();

                slave.Listen();

</code></pre>
						</td>
					</tr>
					<tr id="section_32">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_32">&#182;</a>
							</div>
							<p>prevent the main thread from exiting</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                Thread.Sleep(Timeout.Infinite);
            }
        }

</code></pre>
						</td>
					</tr>
					<tr id="section_33">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_33">&#182;</a>
							</div>
							<p>Modbus TCP master and slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ModbusTcpMasterReadInputsFromModbusSlave()
        {
            byte slaveId = 1;
            int port = 502;
            IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });

</code></pre>
						</td>
					</tr>
					<tr id="section_34">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_34">&#182;</a>
							</div>
							<p>create and start the TCP slave</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            TcpListener slaveTcpListener = new TcpListener(address, port);
            slaveTcpListener.Start();
            ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveId, slaveTcpListener);
            Thread slaveThread = new Thread(slave.Listen);
            slaveThread.Start();

</code></pre>
						</td>
					</tr>
					<tr id="section_35">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_35">&#182;</a>
							</div>
							<p>create the master</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            TcpClient masterTcpClient = new TcpClient(address.ToString(), port);
            ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient);

            ushort numInputs = 5;
            ushort startAddress = 100;

</code></pre>
						</td>
					</tr>
					<tr id="section_36">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_36">&#182;</a>
							</div>
							<p>read five register values</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            ushort[] inputs = master.ReadInputRegisters(startAddress, numInputs);

            for (int i = 0; i &lt; numInputs; i++)
                Console.WriteLine(&quot;Register {0}={1}&quot;, startAddress + i, inputs[i]);

</code></pre>
						</td>
					</tr>
					<tr id="section_37">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_37">&#182;</a>
							</div>
							<p>clean up</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>            masterTcpClient.Close();
            slaveTcpListener.Stop();

</code></pre>
						</td>
					</tr>
					<tr id="section_38">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_38">&#182;</a>
							</div>
							<p>output
Register 100=0
Register 101=0
Register 102=0
Register 103=0
Register 104=0</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        }

</code></pre>
						</td>
					</tr>
					<tr id="section_39">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_39">&#182;</a>
							</div>
							<p>Modbus serial ASCII master and slave example.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ModbusSerialAsciiMasterReadRegistersFromModbusSlave()
        {
            using (SerialPort masterPort = new SerialPort(&quot;COM1&quot;))
            using (SerialPort slavePort = new SerialPort(&quot;COM2&quot;))
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_40">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_40">&#182;</a>
							</div>
							<p>configure serial ports</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                masterPort.BaudRate = slavePort.BaudRate = 9600;
                masterPort.DataBits = slavePort.DataBits = 8;
                masterPort.Parity = slavePort.Parity = Parity.None;
                masterPort.StopBits = slavePort.StopBits = StopBits.One;
                masterPort.Open();
                slavePort.Open();

</code></pre>
						</td>
					</tr>
					<tr id="section_41">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_41">&#182;</a>
							</div>
							<p>create modbus slave on seperate thread</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                byte slaveId = 1;
                ModbusSlave slave = ModbusSerialSlave.CreateAscii(slaveId, slavePort);
                Thread slaveThread = new Thread(new ThreadStart(slave.Listen));
                slaveThread.Start();

</code></pre>
						</td>
					</tr>
					<tr id="section_42">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_42">&#182;</a>
							</div>
							<p>create modbus master</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(masterPort);

                master.Transport.Retries = 5;
                ushort startAddress = 100;
                ushort numRegisters = 5;

</code></pre>
						</td>
					</tr>
					<tr id="section_43">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_43">&#182;</a>
							</div>
							<p>read five register values</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                for (int i = 0; i &lt; numRegisters; i++)
                    Console.WriteLine(&quot;Register {0}={1}&quot;, startAddress + i, registers[i]);
            }

</code></pre>
						</td>
					</tr>
					<tr id="section_44">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_44">&#182;</a>
							</div>
							<p>output
Register 100=0
Register 101=0
Register 102=0
Register 103=0
Register 104=0</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        }

</code></pre>
						</td>
					</tr>
					<tr id="section_45">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_45">&#182;</a>
							</div>
							<p>Write a 32 bit value.</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>        public static void ReadWrite32BitValue()
        {
            using (SerialPort port = new SerialPort(&quot;COM1&quot;))
            {
</code></pre>
						</td>
					</tr>
					<tr id="section_46">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_46">&#182;</a>
							</div>
							<p>configure serial port</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                port.BaudRate = 9600;
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

</code></pre>
						</td>
					</tr>
					<tr id="section_47">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_47">&#182;</a>
							</div>
							<p>create modbus master</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);

                byte slaveId = 1;
                ushort startAddress = 1008;
                uint largeValue = UInt16.MaxValue + 5;

                ushort lowOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(largeValue), 0);
                ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(largeValue), 2);

</code></pre>
						</td>
					</tr>
					<tr id="section_48">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_48">&#182;</a>
							</div>
							<p>write large value in two 16 bit chunks</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                master.WriteMultipleRegisters(slaveId, startAddress, new ushort[] { lowOrderValue, highOrderValue });

</code></pre>
						</td>
					</tr>
					<tr id="section_49">
						<td class="docs">
							<div class="pilwrap">
								<a class="pilcrow" href="#section_49">&#182;</a>
							</div>
							<p>read large value in two 16 bit chunks and perform conversion</p>

						</td>
						<td class="code">
							<pre><code class='prettyprint'>                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, 2);
                uint value = ModbusUtility.GetUInt32(registers[1], registers[0]);
            }
        }
    }
}
</code></pre>
						</td>
					</tr>
			</tbody>
		</table>
	</div>
</body>
</html>
